# _*_ coding:utf-8 _*_
import socket
import os
import numpy
import cv2 as cv
from PIL import ImageGrab
from PIL import Image
from PIL import ImageFilter
from io import BytesIO
import threading

class DesktopServer:
    def __init__(self,ip,port):
        self.ip = ip
        self.port = port


    def jiep_recv(self):
        socket_0 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        socket_0.bind((self.ip, self.port))
        while True:
            try:
                img_data, address = socket_0.recvfrom(1024*1024)
            except Exception as e:
                cv.destroyAllWindows()
            else:
                # 申请内存块，并将收到的UDP数据放进内存块
                buf = BytesIO(img_data)
                # 将收到的UDP数据转为数组
                img_data = numpy.array(Image.open(buf))
                # 这一步作用重要，image类颜色格式为RGB，cv图片颜色格式为BGR故要将颜色格式进行转化，否则将色变
                img_data_1 = cv.cvtColor(img_data, cv.COLOR_RGB2BGR)
                img_data_1 = cv.resize(img_data_1, (850, 500),)
                # 显示图片
                cv.imshow("Service", img_data_1)
                buf.close()
                if cv.waitKey(10)==ord("q"):
                    cv.destroyAllWindows()
                    break


    def run(self):
        port = 5000
        if port > 65534:
            return
        else:
            t1 = threading.Thread(target=self.jiep_recv)
            t1.start()
# 开发需要，用了一个新线程


if __name__ == '__main__':
    # _winmains_()
    pass
